import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class FileObject implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// ----------------------------------------------------------

	// ----------------------------------------------------------
	int flag = 0;
	public String doctorName;
	public String fileName;
	public String dir;
	public String fullPath;
	public String patientID;
	public int port = PrefProperties.myInputPort;
	
	// ----------------------------------------------------------
	public String toSendPath;

	public byte fileContent[];

	public FileObject() {}

	public FileObject(String doctorName, int flag, String fileName) {
		super();

		this.doctorName = doctorName;
		this.flag = flag;
		File file = new File(fileName);
		this.fileName = file.getName();
		this.fullPath = file.getAbsolutePath();
		this.dir = FileOperation.getPathOnly(file.getAbsolutePath());
		this.patientID = file.getParentFile().getName();
		loadFileIntoMemory(file);
	}
	
	public FileObject(String doctorName, int flag) {
		super();

		this.doctorName = doctorName;
		this.flag = flag;
		this.fileName = null;
		this.fullPath = null;
		this.dir = null;
		this.patientID = null;
		this.fileContent = null;
	}

	/* Load the file into an array of bytes */
	private void loadFileIntoMemory(File file) {

		FileInputStream fin = null;
		try {

			// create FileInputStream object
			fin = new FileInputStream(file);

			fileContent = new byte[(int) file.length()];

			// Reads up to certain bytes of data from this
			// input stream into an array of bytes.
			fin.read(fileContent);
			// create string from byte array
			String s = new String(fileContent);
			// System.out.println("File content: " + s);
		} catch (FileNotFoundException e) {
			System.out.println("File not found" + e);
		} catch (IOException ioe) {
			System.out.println("Exception while reading file " + ioe);
		} finally {
			// close the streams using close method
			try {
				if (fin != null) {
					fin.close();
				}
			} catch (IOException ioe) {
				System.out.println("Error while closing stream: " + ioe);
			}
		}
	}

	/* Write a file from an arry of bytes*/
	public void writeFile(String fileName) {

		File file = new File(fileName);

		FileOutputStream fos = null;

		try {

			fos = new FileOutputStream(file);

			// Writes bytes from the specified byte array to this file output
			// stream
			fos.write(fileContent);

		} catch (FileNotFoundException e) {
			System.out.println("File not found" + e);
		} catch (IOException ioe) {
			System.out.println("Exception while writing file " + ioe);
		} finally {
			// close the streams using close method
			try {
				if (fos != null) {
					fos.close();
				}
			} catch (IOException ioe) {
				System.out.println("Error while closing stream: " + ioe);
			}

		}

	}
}
